package com.xht.security.core.userdetails;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * <h1>描述 ：用户信息扩展</h1>
 *
 * @author : 小糊涂
 * @version : 1.0
 **/
public class XhtUser extends User implements OAuth2AuthenticatedPrincipal {

    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    private final Map<String, Object> attributes = new HashMap<>();

    /**
     * 用户ID
     */
    @Getter
    @JsonSerialize(using = ToStringSerializer.class)
    private final Long id;

    /**
     * 部门ID
     */
    @Getter
    @JsonSerialize(using = ToStringSerializer.class)
    private final Long deptId;

    /**
     * 手机号
     */
    @Getter
    private final String phone;

    /**
     * 使用所需的详细信息构造<code>User</code>
     * {@link DaoAuthenticationProvider}.
     *
     * @param username             用户名
     * @param password             密码
     * @param enabled               如果用户已启用，则设置为<code>true</code>
     * @param accountNonExpired     如果帐户未过期，则设置为<code>true</code>
     * @param credentialsNonExpired 如果凭据未过期，则设置为<code>true</code>
     * @param accountNonLocked      如果未锁定，则设置为<code>true</code>
     * @param authorities           如果调用者提供了正确的用户名和密码并且启用了该用户，则应该授予给调用者的权限。非空。
     */
    public XhtUser(Long id, Long deptId, String username, String password, String phone, boolean enabled,
                   boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked,
                   Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.id = id;
        this.deptId = deptId;
        this.phone = phone;
    }

    /**
     * Get the OAuth 2.0 token attributes
     * @return the OAuth 2.0 token attributes
     */
    @Override
    public Map<String, Object> getAttributes() {
        return this.attributes;
    }

    @Override
    public String getName() {
        return this.getUsername();
    }

}

